package Controler;

import Module.BlogDao;
import Module.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-11-30
 * Time: 10:41
 * To BE A Better Person-2020-3-2
 */
@WebServlet("/blogDelete")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session= req.getSession(false);
        if(session == null){
            resp.getWriter().write("请登录后再操作");
            resp.setStatus(302);
            resp.setHeader("Location","login.html");
            return;
        }
        User user=(User) session.getAttribute("user");
        if(user == null){
            resp.getWriter().write("请登录后再操作");
            resp.setStatus(302);
            resp.setHeader("Location","login.html");
            return;
        }
        //先通过前端传来的数据拿到blogid
        BlogDao blogDao=new BlogDao();
        String blogid=req.getParameter("blogid");
        if(blogid == null || blogid.equals("")){
            resp.getWriter().write("暂无此文章");
            resp.setStatus(302);
            resp.setHeader("Location","blog_list.html");
            return;
        }
        //通过找到的blogid来删除这篇文章

        try {
            blogDao.delete(Integer.parseInt(blogid));
            resp.setStatus(302);
            resp.setHeader("Location","blog_list.html");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}


